package net.huashitong.index.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.index.dao.IndexInfoDao;
import net.huashitong.index.dataModel.IndexTargetDataModel;
import net.huashitong.index.entity.DepartmentEntity;
import net.huashitong.index.entity.EnterpriseEntity;
import net.huashitong.index.entity.IndexInfoEntity;
import net.huashitong.index.filter.IndexInfoFilter;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexInfoService extends StringPKService<IndexInfoEntity> {

    @Autowired
    private IndexInfoDao dao;

    @Autowired
    private SysTableService table;


    @Override
    protected BaseDao<IndexInfoEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(IndexInfoEntity obj) {
        obj.setSysId(table.updateMaxSysId("t_eco_index", null));
    }

    /**
     * 主查询入口
     *
     * @param filter
     * @return
     */
    public List<IndexInfoFilter> query(IndexInfoFilter filter) {
        return dao.query(filter);
    }


    // 导入excel 数据。
    public void saveImportIndex(MultipartFile file, String source) {

        InputStream ips = null;
        List<IndexInfoEntity> list = new ArrayList<>();
        try {
            ips = file.getInputStream();
            Workbook wb = WorkbookFactory.create(ips);
            Sheet sheet = wb.getSheetAt(0);

            int lastRow = sheet.getPhysicalNumberOfRows();
            for (int i = 1; i < lastRow; i++) {
                Row row = sheet.getRow(i);
                IndexInfoEntity index = new IndexInfoEntity();
                String head = "";

                Cell cell0 = row.getCell(0);
                if (cell0 == null) {
                    cell0 = row.createCell(0);
                }
                //指标名称
                cell0.setCellType(Cell.CELL_TYPE_STRING);
                head = cell0.getStringCellValue();
                if (StringUtils.isNotBlank(head)) {
                    index.setIndexName(head);
                }

                //指标年份
                Cell cell1 = row.getCell(1);
                if (cell1 == null) {
                    cell1 = row.createCell(1);
                }
                cell1.setCellType(Cell.CELL_TYPE_STRING);
                head = cell1.getStringCellValue();
                index.setDatetime(head);

                //指标等级
                Cell cell2 = row.getCell(2);
                if (cell2 == null) {
                    cell2 = row.createCell(2);
                }
                cell2.setCellType(Cell.CELL_TYPE_STRING);
                head = cell2.getStringCellValue();
                index.setIndexLevel(head);

                //父级指标
                Cell cell3 = row.getCell(3);
                if (cell3 == null) {
                    cell3 = row.createCell(3);
                }
                cell3.setCellType(Cell.CELL_TYPE_STRING);
                head = cell3.getStringCellValue();
                index.setParentIndexId(head);

                //牵头单位
                Cell cell4 = row.getCell(4);
                if (cell4 == null) {
                    cell4 = row.createCell(4);
                }
                cell4.setCellType(Cell.CELL_TYPE_STRING);
                head = cell4.getStringCellValue();
                index.setLeadDept(head);

                //责任单位
                Cell cell5 = row.getCell(5);
                if (cell5 == null) {
                    cell5 = row.createCell(5);
                }
                cell5.setCellType(Cell.CELL_TYPE_STRING);
                head = cell5.getStringCellValue();
                index.setRespDept(head);

                //指标时间类型
                Cell cell6 = row.getCell(6);
                if (cell6 == null) {
                    cell6 = row.createCell(6);
                }
                cell6.setCellType(Cell.CELL_TYPE_STRING);
                head = cell6.getStringCellValue();
                index.setDateUnit(StringUtils.isNotBlank(head) ? 1 : Integer.parseInt(head));

                //企业
                Cell cell7 = row.getCell(7);
                if (cell7 == null) {
                    cell7 = row.createCell(7);
                }
                cell7.setCellType(Cell.CELL_TYPE_STRING);
                head = cell7.getStringCellValue();
                index.setEnterprise(head);

                //指标所在乡镇
                Cell cell8 = row.getCell(8);
                if (cell8 == null) {
                    cell8 = row.createCell(8);
                }
                cell8.setCellType(Cell.CELL_TYPE_STRING);
                head = cell8.getStringCellValue();
                index.setTown(head);

                //分管领导
                Cell cell9 = row.getCell(9);
                if (cell9 == null) {
                    cell9 = row.createCell(9);
                }
                cell9.setCellType(Cell.CELL_TYPE_STRING);
                head = cell9.getStringCellValue();
                index.setLeader(head);

                //来源
                index.setSource(source);
                list.add(index);
            }

            if (ObjectUtils.isNotBlank(list)) {
                batchSave(list);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据指标id查询指标实体
     *
     * @param indexInfo
     * @param deptMap
     * @param enterpriseMap
     * @return
     */
    public Map<String, Object> queryBeanById(IndexInfoEntity indexInfo,
                                             Map<String, DepartmentEntity> deptMap,
                                             Map<String, EnterpriseEntity> enterpriseMap) {
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtils.isNotBlank(indexInfo)) {
            map.put("datetime", indexInfo.getDatetime());
            map.put("indexLevel", indexInfo.getIndexLevel());
            map.put("leader", indexInfo.getLeader());
            //牵头单位(有做关联)
            if (StringUtils.isNotBlank(indexInfo.getLeadDept())) {
                DepartmentEntity leadDept = deptMap.get(indexInfo.getLeadDept());
                map.put("leadDept", leadDept.getDeptName());
            } else {
                map.put("leadDept", "");
            }
            //责任单位（没做关联）
            map.put("respDept", indexInfo.getRespDept());
//                if(StringUtils.isNotBlank(indexInfo.getRespDept())){
//                    DepartmentEntity respDept = deptMap.get(indexInfo.getRespDept());
//                    map.put("respDept",respDept.getDeptName());
//                }else{
//                    map.put("respDept","");
//                }
            //乡镇(有做关联)
            if (StringUtils.isNotBlank(indexInfo.getTown())) {
                DepartmentEntity town = deptMap.get(indexInfo.getTown());
                map.put("town", town.getDeptName());
            } else {
                map.put("town", "");
            }
            //企业(有做关联)
            if (StringUtils.isNotBlank(indexInfo.getTown())) {
                EnterpriseEntity enterprise = enterpriseMap.get(indexInfo.getEnterprise());
                map.put("enterprise", enterprise.getEnterpriseName());
            } else {
                map.put("enterprise", "");
            }
            map.put("dateUnit", indexInfo.getDateUnit());
            map.put("indexName", indexInfo.getIndexName());
        }
        return map;
    }

    public List<IndexTargetDataModel> queryByFilter(IndexTargetDataModel filter) {
        return dao.queryByFilter(filter);
    }

}
